﻿$(document).ready(function(){
	getSource();
});
/**
 * 加载数据
 */

function getSource(){
	$("div[class=holder]").myPagination({
		currPage: 1,
		pageNumber: 15,
		cssStyle:'grayr',
		limit : 10,
		panel:{
			tipInfo_on:true,
			tipInfo_css:{
				width:"22px",height:'12px'
			}
		},
		ajax: {
		  on: true,
		  url: "query",
		  dataType: 'json',
		  param:"ffreezed=0&fdeleted=0&page=1",
		  ajaxStart:function(){
			  ZENG.msgbox.show(" 正在加载中，请稍后...", 6, 10000);
		  },onClick:function(page){
			  $.fn.debug(page);
		  },
		  callback:function(data){
			  ZENG.msgbox.hide(); //隐藏加载提示
			  $.fn.debug(data.source);
			  bindGridList(data);
		  }
		}
	}); 
}
function bindGridList(data){
	$("#total").html('总共找到'+data.total+'条数据');
	var html = "";
	if(data.total > 0){
		$.each(data.source,function(i,template){
			html +='<tr id="web'+i+'" class="tableContent" onclick="ischecked(this)">';// ondblclick="editAjax('+template.id+')"
			html +='<td width="3%" style="padding-left: 5px;"><input type="checkbox" name="web" onchange="checkedInput(this)" onclick="ischecked(document.getElementById(\'web'+i+'\'))" id="td'+ i +'" value="'+template.id+'"/></td>';
			html +='<td width="15%">'+template.fname+'</td>';
			html +='<td width="8%">'+template.fauthor+'</td>';
			html +='<td width="8%">'+(template.fdefinition=="HD"?"高清":"标清")+'</td>';
			html +='<td width="10%">'+(template.fwidth+' × '+template.fheight)+'</td>';
			html +='<td width="10%">'+template.ftype+'</td>';
			html +='<td width="10%">'+(template.flayout == 0?"绝对定位":"流式布局")+'</td>';
			html +='<td>'+template.fdesc+'</td></tr>';
		});
	}else{
		html = "<tr><td colspan='8' align='center'>暂无数据</td></tr>";
	}
	$("#templates").html(html);
}
/**
 * 选中子元素
 * @param obj
 */
function check(obj){
	if(obj.checked){
		var checkbox = $("[name='web']");
		checkbox.attr("checked",'true');//全选 
		$("[class='tableContent']").css("background-color","#D8F79D");
	}else{
		$("[name='web']").removeAttr("checked");//取消全选 
		$("[class='tableContent']").css("background-color","");
	}
}
/**
 * 当前选中项
 * @param obj
 */
function checkedInput(obj){
	var id = obj.id.replace("td","");
	if(obj.checked)
		$("#web"+id).css("background-color","#D8F79D");
	else
		$("#web"+id).css("background-color","");
		
}
/**
 * 单击行时，选中的则取消，未选中的则选中
 * @param obj
 */
function ischecked(obj){
	var id = obj.id;
	var trObj = $("#"+id);
	var tdId = "td"+id.replace("web","");
	var objCheck = $("#"+tdId);
	if(objCheck.attr("checked")){
		objCheck.removeAttr("checked");//取消选中 
		trObj.css("background-color","");
	}else{
		objCheck.attr("checked",'true');//选中
		trObj.css("background-color","#D8F79D");
	}
}
/**
 * 获取所有选中的id
 * @returns {Array}
 */
function checkedInfo(){
	var ids = [];
	$("[name='web'][checked]").each(function(){  
		ids.push($(this).val());  
	}); 
	return ids;  
}
/**
 * 添加信息页面
 */
function addTemplate(){
	//打开新增页面
}

/**
 * 修改
 * @param id
 */
function editAjax(id){
	//传入id进行修改
	window.open("/CMS/templateDesigner/templateDesigner.html?id="+id,"编辑模版","");
}
/**
 * 编辑加载数据
 * @param id
 */
function editTemplate(type){
	var ids = checkedInfo();
	if(ids.length == 1){
		if(type){
			copyTemplate(ids[0]);
		}else
			editAjax(ids[0]);
	}else if(ids.length == 0){
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要修改的信息</div>";	
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
		Box("editHtml",10,10,html,footerhtml,901);
	}else if(ids.length > 1){
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;只能修改1条信息！</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'editHtmlcloseBtn\').click();"/>';
		Box("editHtml",10,10,html,footerhtml,901);
	}
}
/**
 * 复制模版
 * @param id
 */
function copyTemplate(id){
	$.ajax({
		url:"copyTemplate",
		data:{"id":id},
		type:"post",
		success:function(data){
			$.growlUI('复制模版成功');
			$('a[title=1]').click();
		},
		error:function(data){
			$.growlUI('复制模版失败'); 
		}
	
	});
}

/**
 * 删除到回收站或冻结数据
 */
function deletedOrfreezed(deleted,freezed){
	var ids = checkedInfo();
	if(ids.length > 0){
		var s = "";
	    for(var i=0;i<ids.length ;i++){
	        s +=ids[i]+",";
	    }
	    if(s != ""){
	    	s = s.substring(0,s.length-1) ;
	    }
		var data = {"ids":s,"fdeleted":deleted,"ffreezed":freezed};
		$.ajax({
			url:"deletedOrfreezed",
			data:data,
			type:"post",
			success:function(data){
				$.growlUI('成功删除数据'+data+'条');
//				getSource();
				$('a[title=1]').click();
			},
			error:function(data){
				$.growlUI('数据删除失败'); 
			}
		
		});
	}else{
		var html = "<div style='vertical-align: middle;'><img src='/CMS/images/box/icon_alert.gif' alt='提示'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请选择要操作的信息</div>";
		var	footerhtml='<input type="image" alt="Cancel" src="/CMS/images/box/cancel_button.gif" class="btn" id="cancel" onclick="$box(\'deleteHtmlcloseBtn\').click();"/>';
		Box("deleteHtml",10,10,html,footerhtml,901);
	}
}
var dialog = {
	showDialog : function() {
		var kdialog = KindEditor.dialog({
			width : 650,
			height : 400,
			title : '添加首页模版信息',
			body : '<div id="txt_source_div" style="padding:10px;height:380px;overflow:auto;overflow-x:hidden;">'+dialog.addHtml()+'</div>',
			shadowMode : true,
			closeBtn : {name : '关闭',click : function(e) {kdialog.remove();}},
			yesBtn : {
				name : '添加',
				click : function(e) {
					 _waiting._show();
					/**
					 * 提交保存信息数据
					 */
					if(formIsNull($("#templateForm"))){
						$("#templateForm").ajaxSubmit({
							url:"addIndexTemplate",
							type:'post',
							success:function(data){
								var res = data;
								if(data == "1"){
									res = "数据添加成功";
									kdialog.remove();
									$('a[title=1]').click();
								}else if(data == "0"){
									res = "请选择上传文件！！";
								}
								$.growlUI(res);
								_waiting._hide();
							},
							error:function(msg){
								$.growlUI('数据保存失败');
							}
						});
					}
					_waiting._hide();
				}
			},
			noBtn : {name : '取消',click : function(e) {kdialog.remove();}}
		});
		
	},
	backgroundDiv : function(obj) {
		var div_id = obj.id;
		if (obj.checked) {
			jQuery("#import_txt_id" + div_id).css("background", "#ccc");
		} else {
			jQuery("#import_txt_id" + div_id).css("background", "#ffffff");
		}
	},
	addHtml : function(){
		var table = '<form action="addIndexTemplate" method="post" name="templateForm" id="templateForm" enctype="multipart/form-data">';
			table += '<div class="tab-page" id="baseinfo" style="display: block;">';
			table += '<table width="630" border="0" align="center" cellpadding="3" cellspacing="1" class="tableborder">';
		    table += '<tr><td width="100">模版名称</td><td><input class="text" name="fname" type="text" id="fname" value="" size="30" style="border: 0;border-bottom: 1px solid #ABADB3;"/></td></tr>';
		    table += '<tr><td height="30">模版别名</td><td height="30"><input class="text" name="falias" type="text" id="falias" value="" size="30" style="border: 0;border-bottom: 1px solid #ABADB3;"/>（请使用英文字母或数字）</td></tr>';
		    table += '<tr><td height="30">解析度</td><td height="30"><select name="fdefinition" style="border: 0;border-bottom: 1px solid #ABADB3;"><option value="HD">高清</option><option value="SD">标清</option></select></td></tr>';
		    table += '<tr><td height="30">水平分辨率</td><td height="30"><input class="text" name="fwidth" type="text" id="fwidth" value="1280" size="10" style="border: 0;border-bottom: 1px solid #ABADB3;"/>px</td></tr>';
		    table += '<tr><td height="30">垂直分辨率</td><td height="30"><input class="text" name="fheight" type="text" id="fheight" value="720" size="10" style="border: 0;border-bottom: 1px solid #ABADB3;"/>px</td></tr>';
		    table += '<tr><td height="30">文件</td><td height="30"><input class="text" name="file" type="file" id="falias" size="10" style="border: 0;border-bottom: 1px solid #ABADB3;"/>（上传压缩文件，压缩文件不包含顶层文件夹）</td></tr>';
		    table += '<tr><td height="30">模版描述</td><td height="30"><textarea name="fdesc" id="fdesc" style="width:350px;height:100px;resize:none"></textarea></td></tr>';
		    table += '</table></div></form>';
	    return table;
	}
};